package teaching.api;

import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import teaching.pojo.*;
import teaching.service.ChapterServise;
import teaching.service.CourseCommentService;
import teaching.service.CourseFileService;
import teaching.service.CourseStudentService;
import teaching.util.AuthAccess;
import teaching.util.Response;
import teaching.util.ResponseResult;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2024/3/9
 * Description:http://localhost:8080/detail/{cid}
 */
@RestController
@RequestMapping("/detail")
@Api(tags = "课程具体相关接口")
public class CourseDetail {
    @Autowired
    private CourseStudentService courseStudentService;
    @Autowired
    private ChapterServise chapterServise;
    @Resource
    private CourseFileService fileService;
    @Resource
    private CourseCommentService commentService;

    @AuthAccess
    @GetMapping("/{cid}")
    public ResponseResult<List<Student>> getCourseStudent(@PathVariable("cid") Long cid) {
        try {
            System.out.println("cid=" + cid);
            List<Student> studentList = courseStudentService.listByCourse(cid);
            System.out.println(studentList);
            return Response.createOkResp(studentList);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.createFailResp(e.getMessage());
        }
    }

    //新建包括有关联知识点的章节
    @PostMapping("/createChapter1")
    public ResponseResult<Object> addChapter(@RequestBody ChapterRequest chapter) {
        System.out.println("pointIds= " + chapter);
        int num = chapterServise.addChapter(chapter);
        return Response.createOkResp("添加成功");
    }

    @GetMapping("/chapter/{cid}")
    public ResponseResult<List<Chapter>> getAllChapter(@PathVariable("cid") Long cid) {
        try {
            System.out.println("cid=" + cid);
            List<Chapter> chapterList = chapterServise.listByCourse(cid);
            System.out.println(chapterList);
            return Response.createOkResp(chapterList);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.createFailResp(e.getMessage());
        }
    }

    @GetMapping("/chapterById/{id}")
    public ResponseResult<Chapter> getChapter(@PathVariable("id") Long id) {
        try {
            Chapter thisChapter = chapterServise.findChapterById(id);
            System.out.println(thisChapter);
            return Response.createOkResp(thisChapter);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.createFailResp(e.getMessage());
        }
    }

    @PutMapping("/chapter")
    public ResponseResult<Object> updateChapter(Chapter newChapter) {
        int num = chapterServise.update(newChapter);
        System.out.println(newChapter.toString());
        return Response.createMsgResp("更新成功");
    }

    @GetMapping("/chapterQuestions/{chapterId}")
    public ResponseResult<Object> getQuestionsByChapter(@PathVariable("chapterId") long chapterId) {
        System.out.println("章节id是" + chapterId);
        List<Questions> questions = chapterServise.findQuestionsByChapterId(chapterId);
        System.out.println("找出来的习题列表：" + questions);
        return Response.createOkResp(questions);
    }

    @GetMapping("/chapterPoint/{chapterId}")
    public ResponseResult<Object> getPointByChapter(@PathVariable("chapterId") long chapterId) {
        List<Point> points = chapterServise.findPointByChapterId(chapterId);
        return Response.createOkResp(points);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult<Object> delete(@PathVariable("id") Long id) {
        int num = chapterServise.delete(id);
        System.out.println("id" + id + "影响行数:" + num);
        return Response.createOkResp("删除成功");
    }

    @GetMapping("/courseFile/{cid}")
    public ResponseResult<Object> courseFile(@PathVariable("cid") long cid) {
        List<CourseFile> courseFiles = fileService.getAll(cid);
        return Response.createOkResp(courseFiles);
    }

    @GetMapping("/comment/{cid}")
    public ResponseResult<Object> comment(@PathVariable("cid") long cid) {
        List<CourseComment> comments = commentService.getAll(cid);
        return Response.createOkResp(comments);
    }

    @PostMapping("/addComment")
    public ResponseResult<Object> addComment(@RequestBody CourseComment comment) {
        System.out.println(comment.toString());
        int res = commentService.addComment(comment);
        return Response.createOkResp("添加成功");
    }
}
